package com.shop.cereshop.app.controller.msg;

import com.shop.cereshop.app.domain.msg.UnreadMsgCountVO;
import com.shop.cereshop.app.domain.notice.SystemNoticeVO;
import com.shop.cereshop.app.page.recommend.InteractiveMsgVO;
import com.shop.cereshop.app.param.sysmsg.ReadAllMsgParam;
import com.shop.cereshop.app.service.msg.SysMsgService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.utils.EmptyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * @description: 系统消息接口
 * @author zdd
 * @date 2025/10/13 09:36
 * @version 1.0
 */
@RestController
@RequestMapping("/sys/msg")
@Slf4j(topic = "MsgController")
@Api(value = "系统消息接口", tags = "系统消息接口")
public class SysMsgController {

    @Resource
    private SysMsgService sysMsgService;

    @ApiOperation(value = "获取未读消息数量统计")
    @GetMapping("/count")
    public Result<UnreadMsgCountVO> getUnreadMsgCount(HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (EmptyUtils.isEmpty(user)) {
            return Result.failed(CoReturnFormat.USER_NOT_LOGIN);
        }
        Long buyerUserId = user.getBuyerUserId();
        return sysMsgService.getUnreadMsgCount(buyerUserId);
    }

    @ApiOperation("设置所有消息已读")
    @PostMapping("/readAll")
    public Result<Boolean> setReadAllMsg(HttpServletRequest request, @Validated @RequestBody ReadAllMsgParam param) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (EmptyUtils.isEmpty(user)) {
            return Result.failed(CoReturnFormat.USER_NOT_LOGIN);
        }
        Long buyerUserId = user.getBuyerUserId();
        return sysMsgService.setReadAllMsg(buyerUserId, param);
    }

    @ApiOperation(value = "获取系统通知/公告列表")
    @GetMapping("/notice/pages")
    public Result<Page<SystemNoticeVO>> getSystemNoticePage (
            @RequestParam(value = "page", defaultValue = "1") @NotNull(message = "参数不能为空") @ApiParam(value = "分页参数", required = true) Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") @NotNull(message = "参数不能为空") @ApiParam(value = "分页参数", required = true) Integer pageSize,
            HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (EmptyUtils.isEmpty(user)) {
            return Result.failed(CoReturnFormat.USER_NOT_LOGIN);
        }
        Long buyerUserId = user.getBuyerUserId();
        return sysMsgService.getSystemNoticePage(page, pageSize, buyerUserId);
    }

    @ApiOperation(value = "获取互动消息列表")
    @GetMapping("/interactive/pages")
    public Result<Page<InteractiveMsgVO>> getInteractiveMsgPage (
            @RequestParam(value = "page", defaultValue = "1") @NotNull(message = "参数不能为空") @ApiParam(value = "分页参数", required = true) Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") @NotNull(message = "参数不能为空") @ApiParam(value = "分页参数", required = true) Integer pageSize,
            HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (EmptyUtils.isEmpty(user)) {
            return Result.failed(CoReturnFormat.USER_NOT_LOGIN);
        }
        Long buyerUserId = user.getBuyerUserId();
        return sysMsgService.getInteractiveMsgPage(page, pageSize, buyerUserId);
    }

}
